// 获取最终需要的路由结构数据 ==> [{path1: record1}, {path2: record2}]
export function createRouterMap(routes, routerOptions = {}) {
  let pathMap = routerOptions;
  routes.forEach(router => {
    addRouterRecord(router, pathMap);
  })
  return pathMap;
}

export function addRouterRecord(router, pathMap, parent) {
  let path = parent ? `${parent.path}/${router.path}` : router.path;
  let record = {
    path: router.path,
    name: router.name,
    component: router.component,
    parent
  }
  if (!pathMap[path]) {
    pathMap[path] = record;
  }
  if (router.children && router.children.length) {
    router.children.forEach(router1 => {
      // 递归
      addRouterRecord(router1, pathMap, record);
    })
  }
}

// 依据location路径获取父子结构数据数组
export function createRoute(record, { path }) {
  let matched = [];
  if (record) {
    while (record) {
      matched.unshift(record);
      record = record.parent;
    }
  }
  return {
    path,
    matched
  }
  // return {
  //   path: '/home/a',
  //   matched: [{ home的record }, { a的record }]
  // }
}